local Msg = require("core.Msg")
local FindResDefine = require("findRes.FindResDefine")
local FindRestExcel = require("excel.FindResource")
local CopyFindExcel = FindRestExcel.copyFind
local ActFindExcel = FindRestExcel.actFind
local ExpFindExcel = FindRestExcel.expFind
local Grid = require("bag.Grid")
local CopyCommon = require("copy.CopyCommon")
local CopyExcel = require("excel.Map").copy
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local ObjHuman = require("core.ObjHuman")
local ShopHelp = require("shop.ShopHelp")
local ActivityDaiExcel = require("excel.Activity").dailyAct
--清空昨天数据
function clearCurStatus(human)
  --活动找回
  for k,v in pairs(ActFindExcel) do
    local data = getActFindType(human, k)
    if data and checkActOpen(k, os.time() - 86400) then
      data.actFindType = 1
      data.actYuanbaoFindType = 1
    end
    if data and checkActOpen(k, os.time() - 86400 * 2) then
      data.actYestdayFindType = 1
      data.actYuanbaoYestdayFindType = 1
    else
      data.actYestdayFindType = 0
      data.actYuanbaoYestdayFindType = 0
    end  
  end
  --副本找回
  for k,v in pairs(CopyFindExcel) do
    local data = getCopyCntInfo(human, k)
    if data then
      local canEnterCnt = (k == 7001) and 2 or CopyExcel[k].enterCount
      data.copyYuanbaoFindTimes = canEnterCnt - 1
      data.copyFreeFindTimes = 1
    end
  end
end

--副本找回数据
function getCopyCntInfo(human, id)
  for i=0, human.db.copyFindLen - 1 do
    if human.db.copyFind[i].id == id then
      return human.db.copyFind[i]
    end 
  end
end
function setCopyCnt(human, id)
  if human.db.copyFindLen >= 12 then
    human.db.copyFindLen = 12
    assert(nil)
  end
  human.db.copyFind[human.db.copyFindLen].id = id
  human.db.copyFindLen = human.db.copyFindLen + 1
  return human.db.copyFind[human.db.copyFindLen - 1] 
end
--清空副本找回数据
function clearCopyData(human, isFree)
  for i=0, human.db.copyFindLen do
    if isFree == 0 then
      human.db.copyFind[i].copyFreeFindTimes = 0
      human.db.copyFind[i].copyYestdayFreeFindTimes = 0      
    else
      human.db.copyFind[i].copyYuanbaoFindTimes = 0
      human.db.copyFind[i].copyYestdayYuanbaoFindTimes = 0
    end
  end
end

--得到副本找回道具列表
function getCopyItem(human, isFree)
  local item = {}
  for k in pairs(CopyFindExcel) do
    local data = getCopyCntInfo(human, k)
    if not data then
      data = setCopyCnt(human, k)
    end
    if isFree == 0 and data.copyYestdayFreeFindTimes + data.copyFreeFindTimes > 0 then
      item[#item + 1] = {}
      item[#item][1] = CopyFindExcel[k].reward[1]
      item[#item][2] = CopyFindExcel[k].reward[2] * (data.copyYestdayFreeFindTimes + data.copyFreeFindTimes)
    elseif isFree ~= 0 and data.copyYuanbaoFindTimes + data.copyYestdayYuanbaoFindTimes > 0 then
      item[#item + 1] = {}
      item[#item][1] = CopyFindExcel[k].reward[1]
      item[#item][2] = CopyFindExcel[k].reward[2] * (data.copyYuanbaoFindTimes + data.copyYestdayYuanbaoFindTimes)
    end
  end
  return item
end
--得到副本找回所需元宝
function getCopyYuanbao(human, isFree)
  local total = 0
  for k in pairs(CopyFindExcel) do
      local data = getCopyCntInfo(human, k)
      if not data then
        data = setCopyCnt(human, k)
      end
      if data.copyYuanbaoFindTimes + data.copyYestdayYuanbaoFindTimes ~= 0 then
        total = total + CopyFindExcel[k].needYuanbao * (data.copyYuanbaoFindTimes + data.copyYestdayYuanbaoFindTimes)
      end
  end
  return total
end
--副本找回总入口
function copyFind(human, isFree, id)
  if id == 999999 then
    local item = getCopyItem(human, isFree, id)
    if #item == 0 then
      return Broadcast.sendBroadcast(human,  Lang.MOUSE, Lang.FIND_RES_HAVE_NOT_TIMES)
    end
    if not BagLogic.checkCanAddItemList(human, item, #item) then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_BAG_NOT_ENOUGH)
    end
    if isFree ~= 0 then
      local needYuanbao = getCopyYuanbao(human, isFree)
      if ObjHuman.getYuanbao(human) < needYuanbao then
        return ShopHelp.onNoRMB(human)
      end
      ObjHuman.decYuanbao(human, needYuanbao, "findRes")
    end
    for i=1, #item do
      BagLogic.addItem(human, item[i][1], 1, item[i][2], "findRes")
    end
    clearCopyData(human, isFree)
  else
    local data = getCopyCntInfo(human, id)
    if not CopyFindExcel[id] then
      return
    end
    if not data then
      data = setCopyCnt(human, id)
    end
    if isFree == 0 then
      if data.copyYestdayFreeFindTimes + data.copyFreeFindTimes <= 0 then
        return
      end
      if not BagLogic.checkCanAddItem(human, CopyFindExcel[id].reward[1], 1, CopyFindExcel[id].reward[2]) then
        Broadcast.sendBroadcast(human,  Lang.MOUSE, Lang.FIND_RES_ERR_BAG_FULL)
        return
      end
      BagLogic.addItem(human, CopyFindExcel[id].reward[1], 1, CopyFindExcel[id].reward[2], "findRes")
      if data.copyYestdayFreeFindTimes ~= 0 then
        data.copyYestdayFreeFindTimes = 0
      else
        data.copyFreeFindTimes = 0
      end
    else
      if data.copyYuanbaoFindTimes + data.copyYestdayYuanbaoFindTimes <= 0 then
        return
      end      
      if not BagLogic.checkCanAddItem(human, CopyFindExcel[id].reward[1], 1, CopyFindExcel[id].reward[2]) then
        Broadcast.sendBroadcast(human,  Lang.MOUSE, Lang.FIND_RES_ERR_BAG_FULL)
        return
      end
      if ObjHuman.getYuanbao(human) < CopyFindExcel[id].needYuanbao then
        return ShopHelp.onNoRMB(human)
      end
      ObjHuman.decYuanbao(human, CopyFindExcel[id].needYuanbao, "findRes")
      BagLogic.addItem(human, CopyFindExcel[id].reward[1], 1, CopyFindExcel[id].reward[2], "findRes")
      if data.copyYestdayYuanbaoFindTimes ~= 0 then
        data.copyYestdayYuanbaoFindTimes = data.copyYestdayYuanbaoFindTimes - 1
      else
        data.copyYuanbaoFindTimes = data.copyYuanbaoFindTimes - 1
      end
    end
  end
  sendCopyFind(human)
end
--副本找回天数
function getCopyFindDay(data)
  local cnt = 0
  if data.copyYestdayYuanbaoFindTimes ~= 0 or data.copyYestdayYuanbaoFindTimes ~= 0 then
    cnt = cnt + 1
  end
  if data.copyFreeFindTimes ~= 0 or data.copyYuanbaoFindTimes ~= 0 then
    cnt = cnt + 1
  end
  return cnt
end
--发送副本找回信息
function sendCopyFind(human, isLogin, isPop)
  local sendMsg = Msg.gc.GC_COPY_FIND_INFO
  local len = 0
  local cache = {}
  local needPop = false
  for k,v in pairs(CopyFindExcel) do
    len = len + 1
    local data = getCopyCntInfo(human, k)
    if not data or not CopyFindExcel[k] then
      data = setCopyCnt(human, k)
    end
    if isLogin then
      data.copyYuanbaoFindTimes = 0
      data.copyFreeFindTimes = 0
      local enterCnt = CopyCommon.getCopyEnterCount(human, k)
      if k == 7001 then          
        enterCnt = enterCnt + CopyCommon.getCopyEnterCount(human, 7011)
        enterCnt = enterCnt + CopyCommon.getCopyEnterCount(human, 7021)
      end
      local canEnterCnt = (k == 7001) and 2 or CopyExcel[k].enterCount
      if canEnterCnt - enterCnt >= 2 and human.db.lv >= CopyExcel[k].level then
        data.copyYuanbaoFindTimes = canEnterCnt - enterCnt - 1
        data.copyFreeFindTimes = 1
      elseif canEnterCnt - enterCnt == 1 and human.db.lv >= CopyExcel[k].level then
        data.copyYuanbaoFindTimes = 1
      end
    end    
    sendMsg.info[len].id = k
    sendMsg.info[len].canFindDay = getCopyFindDay(data)
    sendMsg.info[len].freeCnt = data.copyYestdayFreeFindTimes + data.copyFreeFindTimes
    sendMsg.info[len].yuanbaoCnt = data.copyYuanbaoFindTimes + data.copyYestdayYuanbaoFindTimes
    sendMsg.info[len].reward[0] = 1
    sendMsg.info[len].needYuanbao = v.needYuanbao
    Grid.create(cache, v.reward[1], v.reward[2])
    Grid.makeItem(cache, sendMsg.info[len].reward[1], 0, human)
    sendMsg.info[len].image[0] = 1    
    sendMsg.info[len].image[1] = v.image1    
    if data.copyFreeFindTimes + data.copyYestdayFreeFindTimes + data.copyYuanbaoFindTimes + data.copyYestdayYuanbaoFindTimes ~= 0 then
      needPop = true
      
    end
  end
  sendMsg.isFirstLogin = (human.db.lv > 3 and needPop and isLogin) and 1 or 0
  sendMsg.isFirstLogin = isPop and 1 or 0
  sendMsg.info[0] = len
  Msg.send(sendMsg, human.fd)
end

----------------------------------------------act start--------------------------------------------------------
--活动找回数据
function getActFindType(human, id)
  for i=0, human.db.ActFindLen - 1 do
    if human.db.ActFind[i].id == id then
      return human.db.ActFind[i]
    end 
  end
end
function setActType(human, id)
  if human.db.ActFindLen >= 12 then
    human.db.ActFindLen = 12
    assert(nil)
  end
  human.db.ActFind[human.db.ActFindLen].id = id
  human.db.ActFind[human.db.ActFindLen].actFindType = 0
  human.db.ActFind[human.db.ActFindLen].actYestdayFindType = 0
  human.db.ActFind[human.db.ActFindLen].actYuanbaoFindType = 0
  human.db.ActFind[human.db.ActFindLen].actYuanbaoYestdayFindType = 0
  human.db.ActFindLen = human.db.ActFindLen + 1  
  return human.db.ActFind[human.db.ActFindLen - 1] 
end
--清空副本找回数据
function clearActData(human, type)
  for i=0, human.db.ActFindLen do
    if type == 1 then
      human.db.ActFind[i].actFindType = 0
      human.db.ActFind[i].actYestdayFindType = 0
    else
      human.db.ActFind[i].actYuanbaoFindType = 0
      human.db.ActFind[i].actYuanbaoYestdayFindType = 0
    end
  end
end
--活动找回道具
function getActItem(human, isFree, id)
  local item = {}
  for k in pairs(ActFindExcel) do
    local data = getActFindType(human, k)
    if not data then
      data = setActType(human, k)
    end
    if data.actFindType + data.actYestdayFindType ~= 0 and isFree == 0  then
      item[#item + 1] = {}  
      item[#item][1] = ActFindExcel[k].freeReward[1]
      item[#item][2] = ActFindExcel[k].freeReward[2] * (data.actFindType + data.actYestdayFindType)
    end  
    if data.actYuanbaoFindType + data.actYuanbaoYestdayFindType ~= 0 and isFree ~= 0  then
      item[#item + 1] = {}
      item[#item][1] = ActFindExcel[k].yuanbaoReward[1]
      item[#item][2] = ActFindExcel[k].yuanbaoReward[2] * (data.actYuanbaoFindType + data.actYuanbaoYestdayFindType)
    end     
  end
  return item
end
--活动找回元宝
function getActYuanbao(human, isFree, id)
  local yuanbao = 0
  for k in pairs(ActFindExcel) do
    local data = getActFindType(human, k)
    if not data then
      data = setActType(human, k)
    end
    if data.actYuanbaoFindType + data.actYuanbaoYestdayFindType ~= 0 then
      yuanbao = yuanbao + ActFindExcel[k].needYuanbao * (data.actYuanbaoFindType + data.actYuanbaoYestdayFindType)
    end    
  end
  return yuanbao
end
--活动找回总入口
function actFind(human, isFree, id)
  if id == 999999 then
    local item = getActItem(human, isFree, id)
    if #item == 0 then
      return Broadcast.sendBroadcast(human,  Lang.MOUSE, Lang.FIND_RES_HAVE_NOT_TIMES)
    end
    if not BagLogic.checkCanAddItemList(human, item, #item) then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_BAG_NOT_ENOUGH)
    end
    if isFree ~= 0 then
      local needYuanbao = getActYuanbao(human, isFree)
      if ObjHuman.getYuanbao(human) < needYuanbao then
        return ShopHelp.onNoRMB(human)
      end
      ObjHuman.decYuanbao(human, needYuanbao, "findRes")
    end
    for i=1, #item do
      BagLogic.addItem(human, item[i][1], 1, item[i][2], "findRes")
    end
    clearActData(human, isFree + 1)
  else    
    local data = getActFindType(human, id)
    if not ActFindExcel[id] then
      return
    end
    if not data then
      data = setActType(human, id)
    end
    if isFree == 0 then
      if data.actFindType + data.actYestdayFindType == 0 then
        return
      end
      if not BagLogic.checkCanAddItem(human, ActFindExcel[id].freeReward[1], 1, ActFindExcel[id].freeReward[2]) then
        Broadcast.sendBroadcast(human,  Lang.MOUSE, Lang.FIND_RES_ERR_BAG_FULL)
        return
      end
      BagLogic.addItem(human, ActFindExcel[id].freeReward[1], 1, ActFindExcel[id].freeReward[2], "findRes")
      if data.actYestdayFindType ~= 0 then
        data.actYestdayFindType = 0
      else
        data.actFindType = 0
      end
    else
      if data.actYuanbaoFindType + data.actYuanbaoYestdayFindType == 0 then
        return
      end      
      if not BagLogic.checkCanAddItem(human, ActFindExcel[id].yuanbaoReward[1], 1, ActFindExcel[id].yuanbaoReward[2]) then
        Broadcast.sendBroadcast(human,  Lang.MOUSE, Lang.FIND_RES_ERR_BAG_FULL)
        return
      end
      if ObjHuman.getYuanbao(human) < ActFindExcel[id].needYuanbao then
        return ShopHelp.onNoRMB(human)
      end
      ObjHuman.decYuanbao(human, ActFindExcel[id].needYuanbao, "findRes")      
      BagLogic.addItem(human, ActFindExcel[id].yuanbaoReward[1], 1, ActFindExcel[id].yuanbaoReward[2], "findRes")
      if data.actYuanbaoYestdayFindType ~= 0 then
        data.actYuanbaoYestdayFindType = 0
      else
        data.actYuanbaoFindType = 0
      end
    end
  end
  sendActFind(human)
end

--检查活动可找回几天
function getActCanFindDay(human, data)
  local cnt = 0
  if data.actFindType ~= 0 or data.actYuanbaoFindType ~= 0 then
    cnt = cnt + 1
  end
  if data.actYestdayFindType ~= 0 or data.actYuanbaoYestdayFindType ~= 0 then
    cnt = cnt + 1
  end
  return cnt
end
--d
function checkOpenToday(nWday, vv)
  for k,v in pairs(vv.dayWed) do
    if nWday == k and v == 1 then
      return false
    end
  end
  return true  
end

--检查活动是否开放
function checkActOpen(copyId, time)
  local useTime = time or os.time() - 86400
  local d = os.date("*t", useTime)
  local nWday = (d.wday + 5) % 7 + 1
  for kk,vv in pairs(ActivityDaiExcel) do
    if vv.copyId == copyId then
      if next(vv.dayWed) == nil then
        return true
      end
      return checkOpenToday(nWday, vv)
    end
  end
  return false
end

--发送活动找回信息
function sendActFind(human, isLogin, isPop)
  local sendMsg = Msg.gc.GC_ACT_FIND_INFO
  local len = 0
  local cache = {}
  local needPop = false
  for k,v in pairs(ActFindExcel) do    
    len = len + 1
    local data = getActFindType(human, k)
    if not data then
      data = setActType(human, k)
    end
    if isLogin then
      local enterCnt = CopyCommon.getCopyEnterCount(human, k)
      data.actFindType = 0
      data.actYuanbaoFindType = 0     
      if enterCnt == 0 and human.db.lv >= CopyExcel[k].level and checkActOpen(k) then
        data.actFindType = 1
        data.actYuanbaoFindType = 1
      end
    end
    sendMsg.info[len].id = k
    sendMsg.info[len].freeCnt = data.actFindType + data.actYestdayFindType
    sendMsg.info[len].yuanbaoCnt = data.actYuanbaoFindType + data.actYuanbaoYestdayFindType 
    sendMsg.info[len].reward[0] = 2
    sendMsg.info[len].needYuanbao = v.needYuanbao
    Grid.create(cache, v.freeReward[1], v.freeReward[2])
    Grid.makeItem(cache, sendMsg.info[len].reward[1], 0, human)
    Grid.create(cache, v.yuanbaoReward[1], v.yuanbaoReward[2])
    Grid.makeItem(cache, sendMsg.info[len].reward[2], 0, human)
    sendMsg.info[len].canFindDay = getActCanFindDay(human, data)
    sendMsg.info[len].image[0] = 2    
    sendMsg.info[len].image[1] = v.image1
    sendMsg.info[len].image[2] = v.image2
    if sendMsg.info[len].canFindDay ~= 0 then
      needPop = true
    end
  end
  sendMsg.isFirstLogin = (human.db.lv > 3 and needPop and isLogin) and 1 or 0
  sendMsg.isFirstLogin = isPop and 1 or 0
  sendMsg.info[0] = len
  Msg.send(sendMsg, human.fd)
end

-----------------------------------------------------------------exp find -------------------------------------
function sendExpFind(human)
  local sendMsg = Msg.gc.GC_EXP_FIND_INFO
  local keepTime = os.time() - human.db.lastLogoutTime
  local keepHour = math.floor(keepTime / 3600) + human.db.canFindExpHour
  keepHour = keepHour > 48 and 48 or keepHour
  if keepHour >= 1 and human.db.lastLogoutTime ~= 0 then
    sendMsg.exp = ExpFindExcel[human.db.lv].exp * keepHour
    sendMsg.hour = keepHour
    sendMsg.needYuanbao  = ExpFindExcel[1].needYuanbao
    Msg.send(sendMsg, human.fd)    
  end
  if human.db.lastLogoutTime ~= 0 then
    human.db.canFindExpHour = keepHour
  end
end

function expFind(human, isFree)
  if human.db.canFindExpHour >= 1 then
    if isFree == 0 then
      ObjHuman.addExp(human, ExpFindExcel[human.db.lv].exp * human.db.canFindExpHour)
    else
      if ObjHuman.getYuanbao(human) < ExpFindExcel[1].needYuanbao * human.db.canFindExpHour then
        return ShopHelp.onNoRMB(human)
      end
      ObjHuman.decYuanbao(human, ExpFindExcel[1].needYuanbao * human.db.canFindExpHour, "findRes")      
      ObjHuman.addExp(human, ExpFindExcel[human.db.lv].exp * human.db.canFindExpHour * 2)
    end
    human.db.canFindExpHour = 0
    Broadcast.sendBroadcast(human,  Lang.MOUSE, Lang.FIND_RES_EXP_SUCCESS)
    local sendMsg = Msg.gc.GC_EXP_FIND_INFO
    sendMsg.exp = 0
    sendMsg.hour = 0
    sendMsg.needYuanbao  = ExpFindExcel[1].needYuanbao
    Msg.send(sendMsg, human.fd)
  end
end